package Dao;

import entity.SalesRecord;
import util.DBConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SalesRecordOfSalesDao {

    /**
     * 获取所有用户的消费记录
     *
     * @return 消费记录列表
     */
    public List<SalesRecord> getAllSalesRecords() {
        List<SalesRecord> salesRecords = new ArrayList<>();
        // SQL 查询语句，连接 SalesRecord、ScenicSpot 和 UserInfo 表
        String sql = "SELECT sr.RecordID, sr.UserID, sr.SpotID, sr.TicketQuantity, sr.ConsumptionAmount, sr.SaleTime, "
                   + "ss.SpotName, ui.MemberLevel "
                   + "FROM SalesRecord sr "
                   + "JOIN ScenicSpot ss ON sr.SpotID = ss.SpotID "
                   + "JOIN UserInfo ui ON sr.UserID = ui.UserID";

        try (Connection conn = DBConnection.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {

            while (rs.next()) {
                SalesRecord record = new SalesRecord();
                // 设置基本属性
                record.setRecordID(rs.getInt("RecordID"));
                record.setUserID(rs.getInt("UserID"));
                record.setSpotID(rs.getInt("SpotID"));
                record.setTicketQuantity(rs.getInt("TicketQuantity"));
                record.setConsumptionAmount(rs.getBigDecimal("ConsumptionAmount"));
                record.setSaleTime(rs.getDate("SaleTime"));

                // 设置关联属性
                record.setSpotName(rs.getString("SpotName")); // 景点名称
                record.setMemberLevel(rs.getString("MemberLevel")); // 会员等级

                salesRecords.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return salesRecords;
    }
}